<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="消费满金额/次" prop="monetaryEach">
        <el-input v-model="dataForm.monetaryEach" placeholder="消费金额"></el-input>
      </el-form-item>
      <el-form-item label="消费赠送积分" prop="integralAdd">
        <el-input v-model="dataForm.integralAdd" placeholder="赠送积分"></el-input>
      </el-form-item>
      <el-form-item label="兑换积分" prop="integralConvert">
        <el-input v-model="dataForm.integralConvert" placeholder="兑换积分"></el-input>
      </el-form-item>
      <el-form-item label="兑换金额" prop="moneyConvert">
        <el-input v-model="dataForm.moneyConvert" placeholder="兑换金额"></el-input>
      </el-form-item>
      <!-- <el-form-item label="创建人" prop="author">
        <el-input v-model="dataForm.author" placeholder="创建人"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item label="修改人" prop="modifier">
        <el-input v-model="dataForm.modifier" placeholder="修改人"></el-input>
      </el-form-item>
      <el-form-item label="修改时间" prop="modifyTime">
        <el-input v-model="dataForm.modifyTime" placeholder="修改时间"></el-input>
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        monetaryEach: "",
        integralAdd: "",
        integralConvert: "",
        moneyConvert: "",
        author: "",
        createTime: "",
        modifier: "",
        modifyTime: ""
      },
      dataRule: {
        monetaryEach: [
          { required: true, message: "消费金额/次不能为空", trigger: "blur" }
        ],
        integralAdd: [
          { required: true, message: "积分增值不能为空", trigger: "blur" }
        ],
        integralConvert: [
          { required: true, message: "兑换积分不能为空", trigger: "blur" }
        ],
        moneyConvert: [
          { required: true, message: "兑换金额不能为空", trigger: "blur" }
        ]
        // author: [
        //   { required: true, message: "创建人不能为空", trigger: "blur" }
        // ],
        // createTime: [
        //   { required: true, message: "创建时间不能为空", trigger: "blur" }
        // ],
        // modifier: [
        //   { required: true, message: "修改人不能为空", trigger: "blur" }
        // ],
        // modifyTime: [
        //   { required: true, message: "修改时间不能为空", trigger: "blur" }
        // ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/bus/pointrule/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.monetaryEach = data.pointRule.monetaryEach;
              this.dataForm.integralAdd = data.pointRule.integralAdd;
              this.dataForm.integralConvert = data.pointRule.integralConvert;
              this.dataForm.moneyConvert = data.pointRule.moneyConvert;
              this.dataForm.author = data.pointRule.author;
              this.dataForm.createTime = data.pointRule.createTime;
              this.dataForm.modifier = data.pointRule.modifier;
              this.dataForm.modifyTime = data.pointRule.modifyTime;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/bus/pointrule/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              monetaryEach: this.dataForm.monetaryEach,
              integralAdd: this.dataForm.integralAdd,
              integralConvert: this.dataForm.integralConvert,
              moneyConvert: this.dataForm.moneyConvert,
              author: this.dataForm.author,
              createTime: this.dataForm.createTime,
              modifier: this.dataForm.modifier,
              modifyTime: this.dataForm.modifyTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
